package com.xzh.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzh.model.Schedules;
import com.xzh.model.search.ScheduleDao;
import com.xzh.service.SchedulesService;
import com.xzh.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/schedules")
public class SchedulesApi {
    private SchedulesService schedulesService;
    @Autowired
    public void setSchedulesService(SchedulesService schedulesService) {
        this.schedulesService = schedulesService;
    }

    @GetMapping
    public ResponseEntity<JsonResult<?>> selectAll(@RequestParam(defaultValue = "1") Integer pageNo,
                                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                                   Schedules schedules) {
        Page<Schedules> page = new Page<>(pageNo, pageSize);
        page = schedulesService.selectAll(page, schedules);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    @PostMapping
    public ResponseEntity<JsonResult<?>> add(@RequestBody Schedules schedules) {
        boolean result = schedulesService.save(schedules);
        return ResponseEntity.ok(JsonResult.success(result));
    }

    @DeleteMapping
    public ResponseEntity<JsonResult<?>> delete(@RequestBody Integer[] ids) {
        int count = schedulesService.removeByIds(List.of(ids));
        if( count == 0){
            return ResponseEntity.ok(JsonResult.fail("删除失败"));
        }else {
            return ResponseEntity.ok(JsonResult.success(count));
        }
    }

    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody Schedules schedules) {
        boolean result = schedulesService.updateById(schedules);
        if( result){
            return ResponseEntity.ok(JsonResult.success());
        }else {
            return ResponseEntity.ok(JsonResult.fail("更新失败"));
        }
    }

    @GetMapping("/available")
    public ResponseEntity<JsonResult<?>> selsectList(){
        List<ScheduleDao> list = schedulesService.selsectList();
        return ResponseEntity.ok(JsonResult.success(list));
    }


}
